const B = require('./base');
module.exports = class Controller extends B{
  constructor(request, reply) {
    super();
    
    const responseData = {};

    this.request = request;
    this.reply = reply;
    this.success = (obj) => {
      responseData[saas.config.codeField] = saas.config.defaultCode;
      responseData[saas.config.msgField] = null;
      responseData[saas.config.dataField] = obj;
      this.reply.send(responseData);
    };
    this.fail = function (code, msg) {
      if (arguments.length === 1) {
        msg = code;
        code = null;
      }
      responseData[saas.config.codeField] = saas.isEmpty(code) ? saas.config.handlerErrCode : code;
      responseData[saas.config.msgField] = msg;
      responseData[saas.config.dataField] = null;
      this.reply.send(responseData);
    };

    this.mysql = saas.app.mysql;
    this.service = saas.service;
    this.model = saas.model;
    this.tableName = saas.tableName;
    this.domain = saas.domain;

    this.get = function (key) {
      if (saas.isEmpty(key)) {
        return request && request.query;
      }
      return request && request.query && request.query[key];
    }
    this.post = function (key) {
      if (saas.isEmpty(key)) {
        return request && request.body;
      }
      return request && request.body && request.body[key];
    }
    this.params = function (key) {
      return request && request.params && request.params[key];
    }
    this.ip = request && request.ips.pop();
    this.method = request && request.raw && request.raw.method;
    this.isGet = (request && request.raw && request.raw.method) === 'GET';
    this.isPost = (request && request.raw && request.raw.method) === 'POST';
    this.headers = request && request.headers;
  }
};

